package com.mmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.mmall.common.ServerResponse;
import com.mmall.dao.ShippingMapper;
import com.mmall.dao.ShippingMapperCustom;
import com.mmall.pojo.Shipping;
import com.mmall.service.IShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author cg.hu
 * @version 1.0.0
 * @date 2021-09-28 10:07
 * @description
 * @since 1.0.0
 */
@Service("iShippingService")
public class ShippingServiceImpl implements IShippingService {

    @Autowired
    private ShippingMapper shippingMapper;

    @Autowired
    private ShippingMapperCustom shippingMapperCustom;

    /**
     * @author cg.hu
     * @version 1.0.0
     * @date 2021-09-28 10:20
     * @description    添加收货地址
     **/
    @Override
    public ServerResponse add(Integer userId, Shipping shipping) {
        shipping.setUserId(userId);
        int rowCount = shippingMapper.insert(shipping);
        if (rowCount > 0) {
            Map result = Maps.newHashMap();
            result.put("shippingId", shipping.getId());
            return ServerResponse.createBySuccess(result);
        }
        return ServerResponse.createByErrorMessage("添加收货地址失败");
    }

    /**
     * @author cg.hu
     * @version 1.0.0
     * @date 2021-09-28 10:36
     * @description    删除发货信息
     **/
    @Override
    public ServerResponse del(Integer userId, Integer shippingId) {
        int rowCount = shippingMapperCustom.deleteByShippingIdUserId(userId, shippingId);
        if (rowCount > 0) {
            return ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByErrorMessage("删除失败");
    }

    /**
     * @author cg.hu
     * @version 1.0.0
     * @date 2021-09-28 10:47
     * @description    更新收货信息
     **/
    @Override
    public ServerResponse update(Integer userId, Shipping shipping) {
        shipping.setUserId(userId);
        int rowCount = shippingMapperCustom.updateByShipping(shipping);
        if (rowCount > 0) {
            return ServerResponse.createBySuccessMessage("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    /**
     * @author cg.hu
     * @version 1.0.0
     * @date 2021-09-28 11:00
     * @description    查寻收货信息详情
     **/
    @Override
    public ServerResponse select(Integer userId, Integer shippingId) {
        Shipping shipping = shippingMapperCustom.selectByShippingIdUserId(userId, shippingId);
        if (null == shipping) {
            return ServerResponse.createByErrorMessage("无法查询到该地址");
        }
        return ServerResponse.createBySuccess("查寻收货信息详情成功", shipping);
    }

    /**
     * @author cg.hu
     * @version 1.0.0
     * @date 2021-09-28 11:21
     * @description    分页获取列表数据
     **/
    @Override
    public ServerResponse<PageInfo> list(Integer userId,
                                         int pageNum,
                                         int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Shipping> shippingList = shippingMapperCustom.selectByUserId(userId);
        PageInfo pageInfo = new PageInfo(shippingList);
        return ServerResponse.createBySuccess(pageInfo);
    }
}
